package com.xuexiang.templateproject;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.xuexiang.xui.utils.ResUtils;

import java.util.ArrayList;
import java.util.List;

import static android.view.KeyEvent.KEYCODE_BACK;


public class ViewActivity extends AppCompatActivity implements View.OnClickListener {

    private String url;
    private String vipUrlNameStr;
    private String vipUrlStr;
    private WebView webView;
    private Intent intent;
    private Spinner spinner;
    private List<String> data_list;
    private ArrayAdapter<String> arr_adapter;
    private Spinner  sp;
    private String vipUrl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);
        intent = getIntent();
        url = intent.getStringExtra("url");
        vipUrlNameStr = intent.getStringExtra("vipUrName");
        vipUrlStr = intent.getStringExtra("vipUrl");
        //默认读取本地数据，如果获取数据库查到最新的接口，将替换本地的接口
        String[] vipLines = ResUtils.getStringArray(R.array.spingarr);
        if(!vipUrlNameStr.equals("") && !vipUrlStr.equals("")){
            vipLines = vipUrlNameStr.split(",");
        }
        webView = findViewById(R.id.webView);
        initWebView();
        webView.loadUrl(url);
        ImageView imageView_play = (ImageView) findViewById(R.id.imageview_play);
        imageView_play.setOnClickListener(this);

        spinner = (Spinner) findViewById(R.id.spinner);
        data_list = new ArrayList<String>();
        for(int i=0;vipLines.length>i;i++){
            data_list.add(vipLines[i]);
        }

        //适配器
        arr_adapter= new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data_list);
        //设置样式
        arr_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //加载适配器
        spinner.setAdapter(arr_adapter);

        sp = (Spinner) findViewById(R.id.spinner);
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                //通过点击数组下标，匹配对应url
                vipUrl = "";
                //默认读取本地数据，如果获取数据库查到最新的接口，将替换本地的接口
                String[] vipUrls = ResUtils.getStringArray(R.array.spinner_url);
                if(!vipUrlNameStr.equals("") && !vipUrlStr.equals("")){
                    vipUrls = vipUrlStr.split(",");
                }
                  vipUrl = vipUrls[position];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                String  aa ="";
                // TODO Auto-generated method stub
            }
        });

    }

    private void initWebView(){


        //获取webview的配置
       WebSettings settings = webView.getSettings();
        //配置支持domstorage
        settings.setDomStorageEnabled(true);//启用或禁用DOM缓存
        settings.setAppCacheEnabled(false);//关闭/启用应用缓存
        settings.setSupportZoom(true);//是否可以缩放，默认true
        //settings.setBuiltInZoomControls(false);//是否显示缩放按钮，默认false
        settings.setJavaScriptEnabled(true);
        settings.setAllowContentAccess(true);
        settings.setUseWideViewPort(true);//设置此属性，可任意比例缩放。大视图模式
        settings.setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.canGoBack();
        //同时加载Https和Http混合模式
        if (Build.VERSION.SDK_INT >= 21) { settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW ); }


        webView.setWebViewClient(new WebViewClient(){
            //@Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if( url.startsWith("http:") || url.startsWith("https:") ) {
                    view.loadUrl(url);
                    return true;

                }else {
                    return true;
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageview_play:
                Intent intent = new Intent(getApplicationContext(), PlayActivity.class);
                intent.putExtra("url", vipUrl==""?"https://z1.m1907.cn/?jx=":vipUrl + webView.getUrl());
                startActivity(intent);
                break;
        }

    }

    //按返回键回退网页
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
